package sdk

import (
	"bytes"
	"encoding/json"
	"errors"
	"net/url"
	"strconv"

	"gitee.com/wz-open-platform/wzopen-sdk-go/constants"
	"gitee.com/wz-open-platform/wzopen-sdk-go/model"
	"gitee.com/wz-open-platform/wzopen-sdk-go/tools"
	"github.com/tidwall/gjson"
)

// 获取在线卡设置信息
func (c *client) GetDeviceSettingOnlineCardInfo(req *model.GetDeviceSettingOnlineCardInfoReq) (resp *model.GetDeviceSettingOnlineCardInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceSettingOnlineCardInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取安全设置信息
func (c *client) DeviceSaveMode(req *model.DeviceSaveModeReq) (resp *model.DeviceSaveModeResp, err error) {
	method := "POST"
	postparams := tools.StructToMap(req)
	postparams = c.buildPostParam(postparams)
	postparamBytes, err := json.Marshal(postparams)
	if err != nil {
		return
	}
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/saveMode", method, map[string]string{}, bytes.NewBuffer([]byte(postparamBytes)), nil)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取时间模式设置信息
func (c *client) GetDeviceModeTimeInfo(req *model.GetDeviceModeTimeInfoReq) (resp *model.GetDeviceModeTimeInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceModeTimeInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取时间模式2设置信息
func (c *client) GetDeviceModeTime1Info(req *model.GetDeviceModeTime1InfoReq) (resp *model.GetDeviceModeTime1InfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceModeTime1Info", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取投币设置信息
func (c *client) GetDeviceSettingCoinInfo(req *model.GetDeviceSettingCoinInfoReq) (resp *model.GetDeviceSettingCoinInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceSettingCoinInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 多个设备信息的返回
func (c *client) FindMultiDevices(req *model.FindMultiDevicesReq) (resp *model.FindMultiDevicesResp, err error) {
	method := "POST"
	postparams := tools.StructToMap(req)
	postparams = c.buildPostParam(postparams)
	postparamBytes, err := json.Marshal(postparams)
	if err != nil {
		return
	}
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/findMultiDevices", method, map[string]string{}, bytes.NewBuffer([]byte(postparamBytes)), nil)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 设备添加
func (c *client) DeviceAdd(req *model.DeviceAddReq) (resp *model.DeviceAddResp, err error) {
	method := "POST"
	postparams := tools.StructToMap(req)
	postparams = c.buildPostParam(postparams)
	postparamBytes, err := json.Marshal(postparams)
	if err != nil {
		return
	}
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/add", method, map[string]string{}, bytes.NewBuffer([]byte(postparamBytes)), nil)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取电量模式设置信息
func (c *client) GetDeviceModeWorkInfo(req *model.GetDeviceModeWorkInfoReq) (resp *model.GetDeviceModeWorkInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceModeWorkInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取电量模式2设置信息
func (c *client) GetDeviceModeWork1Info(req *model.GetDeviceModeWork1InfoReq) (resp *model.GetDeviceModeWork1InfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceModeWork1Info", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取功率模式设置信息
func (c *client) GetDeviceModePowerInfo(req *model.GetDeviceModePowerInfoReq) (resp *model.GetDeviceModePowerInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceModePowerInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取功率模式2设置信息
func (c *client) GetDeviceModePower1Info(req *model.GetDeviceModePower1InfoReq) (resp *model.GetDeviceModePower1InfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceModePower1Info", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 设备列表
func (c *client) DeviceFindList(req *model.DeviceFindListReq) (resp *model.DeviceFindListResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("page", strconv.Itoa(req.Page))
	getparams.Add("page_size", strconv.Itoa(req.Page))
	getparams.Add("device_type", strconv.Itoa(int(req.DeviceType)))
	getparams.Add("device_num", req.DeviceNum)
	getparams.Add("iot_phone", req.IotPhone)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/findList", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 设备删除
func (c *client) DeviceDel(req *model.DeviceDelReq) (resp *model.DeviceDelResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/del", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取收费模式信息
func (c *client) DeviceGetMode(req *model.DeviceGetModeReq) (resp *model.DeviceGetModeResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getMode", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取用户设置模式信息
func (c *client) GetDeviceSettingUserInfo(req *model.GetDeviceSettingUserInfoReq) (resp *model.GetDeviceSettingUserInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceSettingUserInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取免打扰设备信息
func (c *client) GetDeviceSettingNotDisturbInfo(req *model.GetDeviceSettingNotDisturbInfoReq) (resp *model.GetDeviceSettingNotDisturbInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceSettingNotDisturbInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 设备详情
func (c *client) DeviceFindOne(req *model.DeviceFindOneReq) (resp *model.DeviceFindOneResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/findOne", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取离线卡设置信息
func (c *client) GetDeviceSettingOfflineCardInfo(req *model.GetDeviceSettingOfflineCardInfoReq) (resp *model.GetDeviceSettingOfflineCardInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceSettingOfflineCardInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}

// 获取安全设置信息
func (c *client) GetDeviceSettingSafetyInfo(req *model.GetDeviceSettingSafetyInfoReq) (resp *model.GetDeviceSettingSafetyInfoResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/getDeviceSettingSafetyInfo", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}
